package com.clw.template.system;

import cn.hutool.system.*;
import com.sun.management.OperatingSystemMXBean;
import lombok.Data;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.text.DecimalFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class SystemHardwareInfo {

    public static SystemMonitor info(){
        RuntimeInfo runtimeInfo = SystemUtil.getRuntimeInfo();
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        RuntimeMXBean runtimeMXBean = SystemUtil.getRuntimeMXBean();
        JavaInfo javaInfo = SystemUtil.getJavaInfo();
        JavaRuntimeInfo javaRuntimeInfo = SystemUtil.getJavaRuntimeInfo();
        HostInfo hostInfo = SystemUtil.getHostInfo();
        SystemMonitor monitor = new SystemMonitor();
        //cup信息
        monitor.setAvailableProcessors(osmxb.getAvailableProcessors());
        monitor.setSystemCpuLoad(osmxb.getSystemCpuLoad() * 100);
        monitor.setSysCpuLoad(osmxb.getProcessCpuLoad() * 100);
        monitor.setFreeCpuLoad((1 - osmxb.getSystemCpuLoad()) * 100);
        //内存信息
        monitor.setTotalPhysicalMemorySize(osmxb.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024);
        monitor.setFreePhysicalMemorySize(osmxb.getFreePhysicalMemorySize() / 1024 / 1024 / 1024);
        monitor.setUsingPhysicalMemorySize(monitor.getTotalPhysicalMemorySize() - monitor.getFreePhysicalMemorySize());
        monitor.setMemoryUsageRate(monitor.getUsingPhysicalMemorySize()/monitor.getTotalPhysicalMemorySize() * 100);
        monitor.setJvmTotalMemory(runtimeInfo.getTotalMemory() / 1024 / 1024);
        monitor.setJvmFreeMemory(runtimeInfo.getFreeMemory() / 1024 / 1024);
        monitor.setJvmUsingMemory(monitor.getJvmTotalMemory() - monitor.getJvmFreeMemory());
        monitor.setJvmUsageRate(monitor.getJvmUsingMemory() / monitor.getJvmTotalMemory() * 100);
        //JAVA虚拟机信息
        monitor.setJvmName(runtimeMXBean.getVmName());
        monitor.setJavaVersion(javaInfo.getVersion());
        monitor.setStartTime(runtimeMXBean.getStartTime());
        monitor.setStartDate(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli() - monitor.getStartTime());
        monitor.setJvmPath(javaRuntimeInfo.getHomeDir());
        monitor.setProjectPath(System.getProperty(SystemUtil.USER_DIR));
        //服务器信息
        monitor.setServerName(hostInfo.getName());
        monitor.setServerSystem(osmxb.getName());
        monitor.setServerAddress(hostInfo.getAddress());
        monitor.setServerArch(osmxb.getArch());
        //磁盘信息
        monitor.setInfo(getInfo());
        return monitor;
    }

    private static DecimalFormat DECIMALFORMAT = new DecimalFormat("#.##");

    /**
     * 获取磁盘使用信息
     *
     * @return
     */
    public static List<Map<String, String>> getInfo() {

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();

        File[] roots = File.listRoots();// 获取磁盘分区列表
        for (File file : roots) {
            Map<String, String> map = new HashMap<String, String>();

            long freeSpace=file.getFreeSpace();
            long totalSpace=file.getTotalSpace();
            long usableSpace=totalSpace-freeSpace;

            map.put("path", file.getPath());
            map.put("freeSpace", freeSpace / 1024 / 1024 / 1024 + "G");// 空闲空间
            map.put("usableSpace", usableSpace / 1024 / 1024 / 1024 + "G");// 已用空间
            map.put("totalSpace",totalSpace / 1024 / 1024 / 1024 + "G");// 总空间
            map.put("percent", DECIMALFORMAT.format(((double)usableSpace/(double)totalSpace)*100)+"%");// 总空间

            list.add(map);
        }

        return list;
    }

}
